<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="medium"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['plan:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['plan:plan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="7">
        <div class="inner-select">
          <span>计划是否开启：</span>
          <el-select
            v-model="queryParams.planToStart"
            placeholder="请选择"
            size="small"
            @change="changePlanIsOpen"
            :clearable="true"
          >
            <el-option
              v-for="dict in dict.type.plan_is_open"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计划名称" align="center" prop="planName" :show-overflow-tooltip="true" />
      <el-table-column label="计划描述" align="center" prop="planDesc" :show-overflow-tooltip="true" />
      <el-table-column label="计划开始时间" align="center" prop="planStartTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划最低完成度" align="center" prop="planMinDegree">
        <template slot-scope="scope">
          <span>{{scope.row.planMinDegree}}%</span>
        </template>
      </el-table-column>
      <el-table-column label="开启计划" align="center" prop="planToStart">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.plan_is_open" :value="scope.row.planToStart" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['plan:plan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['plan:plan:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-claim"
            @click="handleBindCourseList(scope.row)"
            v-hasPermi="['plan:plan:list']"
          >绑定课程</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handleBindUserList(scope.row)"
            v-hasPermi="['plan:plan:list']"
          >绑定用户</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学习计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="计划描述">
          <el-input v-model="form.planDesc" type="textarea" placeholder="请输入计划描述" />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planStartTime">
          <el-date-picker
            clearable
            v-model="form.planStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择计划开始时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndTime">
          <el-date-picker
            clearable
            v-model="form.planEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择计划结束时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="计划最低完成度" prop="planMinDegree">
          <el-tooltip content="完成该学习计划所需的最低学习进度" placement="top">
            <el-slider v-model="form.planMinDegree" :marks="marks"></el-slider>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="是否开启计划" prop="planToStart">
          <el-switch
            v-model="form.planToStart"
            :inactive-value="0"
            :active-value="1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            inactive-text="不开启"
            active-text="开启"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 已绑定课程列表 -->
    <bind-course-list
      :getBindCourseList="bindPlanCourseList"
      :removeBindCourse="bindPlanCourseRemove"
      :bindId="bindCourseId"
      :dialogAttr="bindCourseDialog"
      @openUnBindList="handleUnBindCourseList"
    />
    <!-- 未绑定课程列表 -->
    <un-bind-course-list
      :getUnBindCourseList="unBindPlanCourseList"
      :addUnBindCourse="unBindPlanCourseAdd"
      :unBindId="unBindCourseId"
      :dialogAttr="unBindCourseDialog"
    />

    <!-- 已绑定用户列表 -->
    <bind-user-list
      :getBindUserList="bindPlanUserList"
      :removeBindUser="bindPlanUserRemove"
      :bindId="bindUserId"
      :dialogAttr="bindUserDialog"
      @openUnBindList="handleUnBindUserList"
    />

    <!-- 未绑定用户列表 -->
    <un-bind-user-list
      :getUnBindUserList="unBindPlanUserList"
      :addUnBindUser="unBindPlanUserAdd"
      :unBindId="unBindUserId"
      :dialogAttr="unBindUserDialog"
    />
  </div>
</template>

<script>
import {
  listPlan,
  getPlan,
  delPlan,
  addPlan,
  updatePlan,
  bindPlanCourseList,
  unBindPlanCourseList,
  bindPlanCourseRemove,
  unBindPlanCourseAdd,
  bindPlanUserList,
  unBindPlanUserList,
  bindPlanUserRemove,
  unBindPlanUserAdd
} from "@/api/plan/plan";
import BindUserList from "@/components/User/bindUserList.vue";
import UnBindUserList from "@/components/User/unBindUserList.vue";
import BindCourseList from "@/components/Course/bindCourseList.vue";
import UnBindCourseList from "@/components/Course/unBindCourseList.vue";
export default {
  name: "Plan",
  dicts: ["plan_is_open", "course_is_begin", "sys_user_sex"],
  components: {
    BindUserList,
    UnBindUserList,
    BindCourseList,
    UnBindCourseList
  },
  data() {
    return {
      // planId
      planId: 0,
      // 绑定用户弹框属性
      bindUserDialog: {
        title: "",
        show: false
      },
      // 未绑定用户弹框属性
      unBindUserDialog: {
        title: "",
        show: false
      },
      // 用户id属性
      bindUserId: {
        type: "planId",
        value: 0
      },
      unBindUserId: {
        type: "planId",
        value: 0
      },
      // 绑定课程弹框属性
      bindCourseDialog: {
        title: "",
        show: false
      },
      // 未绑定课程弹框属性
      unBindCourseDialog: {
        title: "",
        show: false
      },
      // 课程id属性
      bindCourseId: {
        type: "planId",
        value: 0
      },
      unBindCourseId: {
        type: "planId",
        value: 0
      },
      // 绑定用户函数
      bindPlanUserList,
      bindPlanUserRemove,
      // 未绑定用户函数
      unBindPlanUserList,
      unBindPlanUserAdd,
      // 绑定课程函数
      bindPlanCourseList,
      bindPlanCourseRemove,
      // 未绑定课程函数
      unBindPlanCourseList,
      unBindPlanCourseAdd,
      // 滑块提示
      marks: {
        60: "60%",
        80: {
          style: {
            color: "#1989FA"
          },
          label: this.$createElement("strong", "80%")
        }
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: undefined,
        planToStart: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planName: [
          { required: true, message: "计划名称不能为空", trigger: "blur" }
        ],
        planStartTime: [
          { required: true, message: "计划开始时间不能为空", trigger: "blur" }
        ],
        planEndTime: [
          { required: true, message: "计划结束时间不能为空", trigger: "blur" }
        ],
        planMinDegree: [
          { required: true, message: "计划最低完成度不能为空", trigger: "blur" }
        ],
        planToStart: [
          { required: true, message: "计划是否开启不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学习计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams)
        .then(response => {
          this.planList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: undefined,
        planName: undefined,
        planDesc: undefined,
        planStartTime: undefined,
        planEndTime: undefined,
        planName: undefined,
        planToStart: 0,
        planMinDegree: 80
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.planToStart = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学习计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const planId = row.planId || this.ids;
      getPlan(planId)
        .then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改学习计划";
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.planId != null) {
            updatePlan(this.form)
              .then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addPlan(this.form)
              .then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal
        .confirm('是否确认删除学习计划编号为"' + planIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delPlan(planIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 计划是否开启 */
    changePlanIsOpen() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 打开已绑定课程弹框 */
    handleBindCourseList(row) {
      this.bindCourseDialog.title = "已绑定课程列表!";
      this.bindCourseDialog.show = true;
      this.bindCourseId.value = row.planId;
      // 绑定planId
      this.planId = row.planId;
    },
    /** 打开未绑定课程弹框 */
    handleUnBindCourseList() {
      this.unBindCourseDialog.title = "未绑定课程列表!";
      this.unBindCourseDialog.show = true;
      this.unBindCourseId.value = this.planId;
    },
    /** 打开已绑定用户弹框 */
    handleBindUserList(row) {
      this.bindUserDialog.title = "已绑定用户列表!";
      this.bindUserDialog.show = true;
      this.bindUserId.value = row.planId;
      // 绑定planId
      this.planId = row.planId;
    },
    /** 打开未绑定用户弹窗 */
    handleUnBindUserList() {
      this.unBindUserDialog.title = "未绑定用户列表!";
      this.unBindUserDialog.show = true;
      this.unBindUserId.value = this.planId;
    }
  }
};
</script>

<style scoped>

.inner-select {
  display: flex;
  justify-content: center;
}
.inner-select span {
  font-size: 16px;
  color: #909399;
  line-height: 32px;
}

</style>
